package com.test.client;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;


import android.widget.TextView;
import android.widget.Toast;

import com.test.testservice.TestAidlInterface;


public class MainActivity extends AppCompatActivity {

    private TestAidlInterface iMyAidlInterface;

    private boolean connected = false;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.tv);
        bind();
    }

    public void getMsg() {
        if (iMyAidlInterface == null) {
            Toast.makeText(this, "服务未连接", Toast.LENGTH_LONG).show();
            return;
        }
        try {
            tv.setText(iMyAidlInterface.getName());
        
        
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }


    public String getLauncherPkg(){
        String pkg = null;
        // 创建 Intent 对象并设置 action 为 "android.intent.action.MAIN"
        Intent intent = new Intent("android.intent.action.MAIN");

        // 添加 category 为 "android.intent.category.HOME"
        intent.addCategory("android.intent.category.HOME");

        // 调用 resolveActivity() 方法获取 ResolveInfo 对象
        ComponentName componentName = intent.resolveActivity(getPackageManager());

        if (componentName != null) {
            try {
                // 根据 package name 获取 PackageInfo 对象
                String packageName = componentName.getPackageName();
                PackageInfo packageInfo = getPackageManager().getPackageInfo(packageName, 0);

                // 输出 launcher 的包名
                pkg =  packageInfo.packageName;
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            pkg = "";
            System.out.println("No default home app found.");
        }
        return pkg;
    }



    private ServiceConnection serviceConnection;

    public void bind() {
       LogUtil.getInstance().d( "bind");
        Intent intent = new Intent();
        intent.setAction(".IMyAidlInterface");
       intent.setComponent(new ComponentName("com.test.server", "com.test.testservice.TestService"));
      //  startService(intent);
        boolean b = bindService(intent, serviceConnection = new ServiceConnection() {

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                 LogUtil.getInstance().d(  "onServiceConnected");
                connected = true;
                iMyAidlInterface = TestAidlInterface.Stub.asInterface(service);
                getMsg();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                 LogUtil.getInstance().d(  "onServiceDisconnected");
                iMyAidlInterface = null;
                connected = false;
            }
        }, BIND_AUTO_CREATE);
         LogUtil.getInstance().d(  "bind:"+b);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(serviceConnection);
    }
}